package com.tzbank.wisdom.utils;

import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.utils.String2Utils;
import com.tzbank.wisdom.constant.SysConstans;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class RequestMap {

    private final static String DATA_TIME_FORMATTER_YYYMMDD = "yyyMMdd";

    private final static String DATA_TIME_FORMATTER_HHMMSSSSS = "HHmmssSSS";

    /**
     * 获取系统报文头 默认来源是 PMBBS <br>
     * consumerId targetSysId 是 ""
     *
     * @param code     服务码
     * @param scene    服务场景
     * @param branchId 机构id
     * @return
     */
    public static Map<String, Object> getReqSysHead(String code, String scene, String branchId) {
        return getReqSysHead(code, scene, branchId, BusinessConstant.SOURCRE_TYPE_PMBBS, "", "");
    }

    /**
     * 获取系统报文头 默认来源是 PMBBS
     *
     * @param code        服务码
     * @param scene       服务场景
     * @param branchId    机构id
     * @param consumerId  消费者id
     * @param targetSysId 访问系统id
     * @return
     */
    public static Map<String, Object> getReqSysHead(String code, String scene, String branchId, String consumerId,
                                                    String targetSysId) {
        return getReqSysHead(code, scene, branchId, BusinessConstant.SOURCRE_TYPE_PMBBS, consumerId, targetSysId);
    }

    /**
     * 获取系统报文头 <br>
     * consumerId targetSysId 是 ""
     *
     * @param code       服务码
     * @param scene      服务场景
     * @param branchId   机构id
     * @param sourceType 来源类型
     * @return
     */
    public static Map<String, Object> getReqSysHead(String code, String scene, String branchId, String sourceType) {
        return getReqSysHead(code, scene, branchId, sourceType, "", "");
    }

    /**
     * 获取系统报文头
     *
     * @param code        服务码
     * @param scene       服务场景
     * @param branchId    机构id
     * @param sourceType  来源类型
     * @param consumerId  消费者id
     * @param targetSysId 访问系统id
     * @return
     */
    public static Map<String, Object> getReqSysHead(String code, String scene, String branchId, String sourceType,
                                                    String consumerId, String targetSysId) {

        Map<String, Object> reqSysHead = new HashMap<String, Object>();
        reqSysHead.put(SysConstans.SYS_TRAN_CODE, code);
        reqSysHead.put(SysConstans.SYS_TRAN_SENCE, scene);

        reqSysHead.put("branchId", branchId);
        reqSysHead.put("consumerId", consumerId);
        reqSysHead.put("targetSysId", targetSysId);

        reqSysHead.put("sourceType", sourceType);
        reqSysHead.put("tranDate", RequestMap.getDateFormatter(DATA_TIME_FORMATTER_YYYMMDD));
        reqSysHead.put("tranTimestamp", RequestMap.getDateFormatter(DATA_TIME_FORMATTER_HHMMSSSSS));
        reqSysHead.put("userId", BusinessConstant.USER_ID_ABP);
        reqSysHead.put("consumerSeqNo", String2Utils.getGenerateUUID());

        return reqSysHead;
    }

    /**
     * @param reqBody
     * @return
     */
    public static Map<String, Object> getInputMap(Map<String, Object> reqBody) {

        Map<String, Object> inputMap = new HashMap<String, Object>();
        String bankSystemType = reqBody.get("bankSystemType").toString();
        String ip = reqBody.get("ip").toString();

        inputMap.put("logonType", "R");
        inputMap.put("clientIp", ip);
        inputMap.put("bankSystemType", bankSystemType);

        return inputMap;
    }

    /**
     * 从 Redis缓存 中获取 用户信息
     *
     * @param clientSeqNo 电子客户号
     * @return
     */
    public static Map<String, Object> getInputUserInfo(String clientSeqNo) {

        Map<String, Object> inputMap = new HashMap<String, Object>();

        Map<?, ?> userMap = RedisUtils.getUserInfo(clientSeqNo);
        if (userMap == null) {
            return ResultUtil.resultMap("请重新登录", ResultCode.RESUTL_CODE_FAILURE.code(), null);
        }
        String clientNo = userMap.get("clientNo").toString();
        String userSeqNo = userMap.get("userSeqNo").toString();
        String selfUserId = userMap.get("selfUserId").toString();
        String selfUserBranch = userMap.get("selfUserBranch").toString();
        inputMap.put("selfUserId", selfUserId);
        inputMap.put("selfUserBranch", selfUserBranch);
        inputMap.put("clientNo", clientNo);
        inputMap.put("eClientNo", clientSeqNo);
        inputMap.put("eUserNo", userSeqNo);

        return inputMap;
    }

    /**
     * 格式化时间
     *
     * @param formatter
     * @return
     */
    private static String getDateFormatter(String formatter) {

        LocalDateTime localDateTime = LocalDateTime.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatter);

        String date = localDateTime.format(dateTimeFormatter);

        return date;

    }

}
